package study.edumanagesystemspringboot.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.edumanagesystemspringboot.config.ResultMap;
import study.edumanagesystemspringboot.entity.Admin;
import study.edumanagesystemspringboot.entity.Counsellor;
import study.edumanagesystemspringboot.entity.Department;
import study.edumanagesystemspringboot.entity.Principal;
import study.edumanagesystemspringboot.service.IAdminService;
import study.edumanagesystemspringboot.service.IStudentService;
import study.edumanagesystemspringboot.service.ITeacherService;
import study.edumanagesystemspringboot.service.impl.IAdminServiceImp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 访问者接口
 *
 * @author zouran
 * createDate：2023/12/27  23:49
 */
@RestController
@RequestMapping("/visitor")
public class VisitorController {
    private final IStudentService iStudentService;
    private final ITeacherService iTeacherService;

    private final ResultMap<Object> resultMap;

    public VisitorController(IStudentService iStudentService, ITeacherService iTeacherService, ResultMap<Object> resultMap) {
        this.iStudentService = iStudentService;
        this.iTeacherService = iTeacherService;
        this.resultMap = resultMap;
    }

    private Map<String, Object> classify(List<Map<String, Object>> list) {
        Map<String, Object> maps = new HashMap<>();
        List<Map<String, Object>> students = new ArrayList<>();
        List<Map<String, Object>> teachers = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : list) {
            if (stringObjectMap.get("identify") == "student") {
                stringObjectMap.remove("identify");
                students.add(stringObjectMap);
            }
            if (stringObjectMap.get("identify") == "teacher") {
                stringObjectMap.remove("identify");
                teachers.add(stringObjectMap);
            }
        }
        maps.put("student", students);
        maps.put("teacher", teachers);
        return maps;
    }

    /**
     * 辅导员视角
     *
     * @return 辅导员视角
     */
    @RequestMapping("/counsellor")
    public ResultMap<Object> counsellorQueryAll() {
        System.out.println(StpUtil.getPermissionList());
        return resultMap.success(classify(
                new Department(iTeacherService.list(null), iStudentService.queryAll()).information(new Counsellor())));
    }

    /**
     * 校长视角
     *
     * @return 校长视角
     */
    @RequestMapping("/principal")
    public ResultMap<Object> principalQueryAll() {
        return resultMap.success(classify(
                new Department(iTeacherService.list(null), iStudentService.queryAll()).information(new Principal())));
    }

}
